package dms.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static SessionFactory sessionFactory;
	private static Configuration cfg;
	static {
		try {
			cfg = new Configuration();
			cfg.configure();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * @return the cfg
	 */
	public static Configuration getCfg() {
		return cfg;
	}

	/**
	 * @param cfg the cfg to set
	 */
	public static void setCfg(Configuration cfg) {
		HibernateUtil.cfg = cfg;
	}

	/**
	 * @param sessionFactory the sessionFactory to set
	 */
	public static void setSessionFactory(SessionFactory sessionFactory) {
		HibernateUtil.sessionFactory = sessionFactory;
	}

	static public SessionFactory getSessionFactory() {
		if (sessionFactory == null) {
			sessionFactory = cfg.buildSessionFactory();
		}
		
		return sessionFactory;
	}
	
	static public void shutdown() {
		SessionFactory sessionFactory = getSessionFactory();
		if (sessionFactory != null) {
			sessionFactory.close();
		}
		
		sessionFactory = null;
	}
	
}
